﻿$(document).ready(function () {
    var img = $(".goods-imgs").children('.small-img').children('li').first().children();
    img.addClass("active");
    var src = $(img)[0].src;
    $('.big-img img').attr('src', src);
    var count = localStorage.count;
    if (!isNaN(count)) {
        $("#count").val(count);
    }
})

$('.small-img li img').click(function () {
    $('.small-img li img').each(function () {
        $(this).removeClass('active')
    });
    $(this).addClass('active');
    var src = $(this)[0].src;
    $('.big-img img').attr('src', src);
})

$('.small-img li img').mouseover(function () {
    var src = $(this)[0].src;
    $('.big-img img').attr('src', src);
}).mouseout(function () {
    var src = $('.active')[0].src;
    $('.big-img img').attr('src', src);
})

$("#add").click(function () {
    var count = checkVal($("#count").val());
    if (count == 0) {
        $("#count").val(0);
    }

    $("#count").val(count+1);
})

$("#reduce").click(function () {
    var count = checkVal($("#count").val());
    if (count == 0) {
        $("#count").val(0);
    }
    if (count != 1) {
        $("#count").val(count - 1);
    }
})

$("#count").blur(function () {
    var value = $("#count").val()
    if (value == "" || isNaN(value)) {
        $("#count").val(1);
        localStorage.count = 1;
    }
    localStorage.count = value;

})

$("#submit-bt").click(function () {
    $("form").submit();
})

function checkVal(val){
    var value = parseInt(val);
    if (value == NaN) {
        return 0;
    }
    else return value;
}